package com.bosssoft.trainee.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.trainee.common.CodeMsg;
import com.bosssoft.trainee.common.Result;
import com.bosssoft.trainee.entity.dto.UserDTO;
import com.bosssoft.trainee.entity.po.User;
import com.bosssoft.trainee.entity.vo.UserQueryVO;
import com.bosssoft.trainee.entity.vo.UserVO;
import com.bosssoft.trainee.service.TokenService;
import com.bosssoft.trainee.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuqiang
 * @date 2022/7/13 15:25
 */
@RestController
@Slf4j
@RequestMapping("userInfo")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    TokenService tokenService;


    /**
     * 获取用户信息列表，展示在用户信息界面中
     *
     * @return 统一消息
     */
    @GetMapping("/list")
    public Result<Object> getUserInfo() {

        List<User> list = userService.list();
        List<UserVO> listVo = new ArrayList<>();
        for (User user : list) {
            UserVO userVo = new UserVO();
            BeanUtils.copyProperties(user, userVo);
            listVo.add(userVo);
        }
        Integer id = Integer.valueOf(tokenService.getUserId());
        log.info("当前用户id:{}", id);
        Map<String,Object> map = new HashMap<>();
        map.put("total",listVo.size());
        map.put("items",listVo);
        return Result.success(map);
//        return Result.success(listVo);
    }

    /**
     * 修改
     *
     * @param userVO UserVO
     * @return 统一消息
     */
    @PostMapping("/updateUser")
    public Result<Object> updateUser(@Valid @RequestBody UserVO userVO) {
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(userVO, userDTO);
        log.info("修改用户传参userVo：{}",userVO);
        log.info("修改用户传参userVo：{}",userDTO);
        userDTO.setUpdateId(Integer.valueOf(tokenService.getUserId()));
        if (userService.updateUser(userDTO)) {
            return Result.success();
        } else {
            return Result.error(CodeMsg.USER_NOT_EXSIST);
        }
    }

    /**
     * 添加用户
     *
     * @return 统一消息
     */
    @PostMapping("/insertUser")
    public Result<Object> insertUser(@Valid @RequestBody UserVO userVo) {
        UserDTO userDto = new UserDTO();
        log.info("新增用户-传参userVo：{}",userVo);
        BeanUtils.copyProperties(userVo, userDto);
        //更新人id
        userDto.setUpdateId(Integer.valueOf(tokenService.getUserId()));
        userService.saveUser(userDto);
        return Result.success();
    }

    /**
     * 注册
     *
     * @return 统一消息
     */
    @PostMapping("/register")
    public Result<Object> register(@Valid @RequestBody UserVO userVo) {
        UserDTO userDto = new UserDTO();
        log.info("新增用户-传参userVo：{}",userVo);
        BeanUtils.copyProperties(userVo, userDto);
        //更新人id
//        userDto.setUpdateId(Integer.valueOf(tokenService.getUserId()));
        userService.saveUser(userDto);
        return Result.success();
    }

    /**
     * 删除用户
     *
     * @return 统一消息
     */
    @GetMapping("/delete")
    public Result<Object> deleteUser(String id) {

        User user = userService.getById(Integer.valueOf(id));
        log.info("删除用户拿到的的id:{}",id);
        if (user == null) {
            return Result.error(CodeMsg.USER_NOT_EXSIST);
        } else {
            userService.removeById(Integer.valueOf(id));
            return Result.success();
        }
    }

    /**
     * 查询根据id
     *
     * @return 统一消息
     */
    @GetMapping("/find")
    public Result<Object> findById(Integer id) {

        User user = userService.getById(id);
        UserVO userVo = new UserVO();
        if (user == null) {
            return Result.error(CodeMsg.USER_NOT_EXSIST);
        } else {
            BeanUtils.copyProperties(user, userVo);
            return Result.success(userVo);
        }
    }


    /**
     * 多条件+模糊查询
     *
     * @param userQueryVO
     * @return
     */
    @GetMapping("/findUser")
    public Result<Object> findUser(UserQueryVO userQueryVO) {

        List<UserDTO> list = userService.findUser(userQueryVO);
        log.info("查找用户-传参userQueryVo：{}",userQueryVO);
        log.info("查找用户-查找user:{}",list);
        if (list.isEmpty()) {
//            return Result.error(CodeMsg.USER_NOT_EXSIST);
            Map<String,Object> map = new HashMap<>();
            map.put("total",list.size());
            map.put("items","");
            return Result.success(map);
        } else {
            List<UserVO> userVOList = new ArrayList<>();
            for (UserDTO userDTO : list) {
                UserVO userVO = new UserVO();
                BeanUtils.copyProperties(userDTO, userVO);
                userVOList.add(userVO);
            }
            Map<String,Object> map = new HashMap<>();
            map.put("total",list.size());
            map.put("items",userVOList);
//            return Result.success(userVOList);
            return Result.success(map);
        }
    }

    /**
     * 分页
     *
     * @param pageNum  pageNum
     * @param pageSize pageSize
     * @return result
     */
    @GetMapping("/test")
    public Result<Object> getUserList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        List<UserDTO> list = userService.getListDTO(pageNum, pageSize);
        List<UserVO> listVO = new ArrayList<>();
        for (UserDTO userDTO : list) {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(userDTO, userVO);
            listVO.add(userVO);
        }
        Page<User> pageInfo = new Page<>(pageNum, pageSize);
        userService.page(pageInfo);

        Map<String, Object> map = new HashMap<>();
        map.put("items", listVO);
        map.put("pages", pageInfo.getPages());
        map.put("total", pageInfo.getTotal());
        map.put("currentPage", pageInfo.getCurrent());

        return Result.success(map);
    }

}
